using DA.AnimGraph;
using OdinSerializer;
using System.Collections.Generic;

namespace DA.AnimGraph.Editor {

    internal class AGANode_Clip : AGANode_SampleBase, IResourceNode {

        public override AGANodeModelBase modelProperty { set => model = (AGANodeModel_Clip)value; get => model; }
        [OdinSerialize] AGANodeModel_Clip model;

        public AGANode_Clip(AGANodeModelBase model) : base(model) {
        }

        public override int Publish(List<PublishNode> outNodes, AGAPublisher.PublishContext context) {
            var pNode = new ClipPNode();
            pNode.uuid = uuid;
            pNode.data = model.Publish(context);
            var index = outNodes.Count;
            outNodes.Add(pNode);
            PublishPerks(pNode, context);
            return index;
        }

        public AUID[] GetResources() {
            return model.GetResources();
        }
        public void ReplaceResourceAUID(Dictionary<AUID, AUID> uuidMapping) {
            model.ReplaceResourceAUID(uuidMapping);
        }
    }


    [ModelOf(typeof(AGANode_Clip))]
    internal class AGANodeModel_Clip : AGANodeModelBase {

        public AGAFieldModel_ClipLink clip = new();
        public AGAFieldModel_FloatLink playbackSpeed = new() { defaultValue = 1 };

        public ClipNode.SetupData Publish(AGAPublisher.PublishContext context) {
            return new ClipNode.SetupData() {
                clipDesc = clip.Publish(context),
                playbackSpeedDesc = playbackSpeed.Publish(context),
            };
        }
        public AUID[] GetResources() {
            if (clip.clipKey.isValid) {
                return new AUID[] { clip.clipKey };
            }
            return null;
        }
        public void ReplaceResourceAUID(Dictionary<AUID, AUID> uuidMapping) {
            if (uuidMapping.TryGetValue(clip.clipKey, out var newId)) {
                clip.clipKey = newId;
            }
        }
    }


}
